﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Reports_ServiceInvoice_InvoiceViews_AdvanceReceipts : System.Web.UI.Page
{

    Datautility DU = new Datautility();
    AccountSettings acntstng = new AccountSettings();
    DataTable dtMain = new DataTable();
    string finyear = "";
   
    Double totalTaxable = 0;
    Double totalReceiptAmount = 0;
    Double totalServiceTax = 0;
    Double totalPECess = 0;
    Double totalHECess = 0;
    Double totalReceiptAmountN = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            finyear = Session["finyear"].ToString();
            hidfinyear.Value = Session["finyear"].ToString();
            txtFromDate.Text = Convert.ToString("01/04/" + finyear);
            txtToDate.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            BindGrid(txtFromDate.Text, txtToDate.Text);
            lblCompName.Text = Session["compname"].ToString();
            lblCompName2.Text = Session["compname"].ToString();
        }
    }
    protected void BindGrid(string from, string to)
    {
        if (from != string.Empty && to != string.Empty)
        {
            SqlParameter[] sqlparam = {
                                      new SqlParameter("@Finyear",hidfinyear.Value.ToString()),
                                      new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(from))),
                                      new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(to))),
                                  };

            dtMain = DU.getdatausingprocedure("USP_IVC_GetAdvanceReceiptDetails", sqlparam);
        }
        lblMsg.Text = "Advance Receipts Register : From " +string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(from)) + " To " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(to)) + ".";
        grdAdvanceReceiptView.DataSource = dtMain;
        grdAdvanceReceiptView.DataBind();
        grdPrint.DataSource = dtMain;
        grdPrint.DataBind();
    }

    protected void grdAdvanceReceiptView_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double TaxableAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TaxableAmount"));
            Double ReceiptAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptAmount"));
            Double ServiceTax1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ServiceTax1"));
            Double PECess1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "PECess1"));
            Double HECess1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "HECess1"));


            totalTaxable = totalTaxable + Math.Abs(TaxableAmount);
            totalReceiptAmount = totalReceiptAmount + Math.Abs(ReceiptAmount);
            totalHECess = totalHECess + Math.Abs(HECess1);
            totalPECess = totalPECess + Math.Abs(PECess1);
            totalServiceTax = totalServiceTax + Math.Abs(ServiceTax1);

            if (Math.Abs(ReceiptAmount) > 0)
            {
                e.Row.Cells[5].Text = acntstng.GetCurrency(Math.Abs(ReceiptAmount));
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }
            if (Math.Abs(TaxableAmount) > 0)
            {
                e.Row.Cells[6].Text = acntstng.GetCurrency(Math.Abs(TaxableAmount));
            }
            else
            {
                e.Row.Cells[6].Text = string.Empty;
            }

            if (Math.Abs(ServiceTax1) > 0)
            {
                e.Row.Cells[7].Text = acntstng.GetCurrency(Math.Abs(ServiceTax1));
            }
            else
            {
                e.Row.Cells[7].Text = string.Empty;
            }
            if (Math.Abs(PECess1) > 0)
            {
                e.Row.Cells[8].Text = acntstng.GetCurrency(Math.Abs(PECess1));
            }
            else
            {
                e.Row.Cells[8].Text = string.Empty;
            }
            if (Math.Abs(HECess1) > 0)
            {
                e.Row.Cells[9].Text = acntstng.GetCurrency(Math.Abs(HECess1));
            }
            else
            {
                e.Row.Cells[9].Text = string.Empty;
            }

            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Right;


        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[4].Text = "Total";
            e.Row.Cells[5].Text = acntstng.GetCurrency(totalReceiptAmount).ToString();
            e.Row.Cells[6].Text = acntstng.GetCurrency(totalTaxable).ToString();
            e.Row.Cells[7].Text = acntstng.GetCurrency(totalServiceTax).ToString();
            e.Row.Cells[8].Text = acntstng.GetCurrency(totalPECess).ToString();
            e.Row.Cells[9].Text = acntstng.GetCurrency(totalHECess).ToString();
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.VerticalAlign = VerticalAlign.Bottom;

        }
    }
    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }

    protected void grdAdvanceReceiptView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdAdvanceReceiptView.PageIndex = e.NewPageIndex;
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    protected void grdPrint_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           Double ReceiptAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptAmount"));
            
            totalReceiptAmountN = totalReceiptAmountN + Math.Abs(ReceiptAmountN);

            if (Math.Abs(ReceiptAmountN) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(ReceiptAmountN));
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[3].Text = "Total";
            e.Row.Cells[4].Text = acntstng.GetCurrency(totalReceiptAmountN).ToString();
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.VerticalAlign = VerticalAlign.Bottom;

        }
    }
}
